package views.seeker;

import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.DBUtil;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/*ok*/
public class AllSeekers extends JFrame {
    private JScrollPane jsp = new JScrollPane();
    ImageIcon imageIcon = new ImageIcon("src/images/seeker.png");
    private JTable table = new JTable();
    private MyModel model;
    private static final Logger LOGGER = LoggerFactory.getLogger(AllSeekers.class);
    private String[] table_title = {"身份证号", "姓名", "性别", "年龄", "联系电话", "聘用情况"};

    public AllSeekers() {
        setTitle("显示所有求职者");
        setSize(500, 400);
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        setResizable(false); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        setLocationRelativeTo(null);

        model = new MyModel();
        table.setModel(model);
        for (int i = 0; i < table_title.length; i++) {
            table.getColumnModel().getColumn(i).setHeaderValue(table_title[i]);
        }
        //设置表格中内容居中显示
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);
        // 设置可见视图的接口
        jsp.setViewportView(table);
        // 定义表格 宽500，高度400
        jsp.setPreferredSize(new Dimension(500, 400));
        // 设置横向和垂直滚动条可见
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        this.setLayout(new BorderLayout());
        this.add(jsp);
    }

    class MyModel extends AbstractTableModel {
        private int row;
        private int column;
        private ResultSet rs;
        private Statement stmt;
        @Language("MySQL")
        private String sql = " select identityNum, name, gender, age, contact_phone, ishired from job_seeker; ";

        public MyModel() {
            stmt = DBUtil.getStatement();
            try {
                rs = stmt.executeQuery(sql);
                rs.last();// 将光标移到最后一行
                row = rs.getRow();// 获取行号(最大行索引)
                ResultSetMetaData rsmd = rs.getMetaData();// 通过结果集对象来获取
                column = rsmd.getColumnCount();// 获取列数
            } catch (SQLException e) {
                LOGGER.info("错误信息：" + e.getMessage());
            }
        }

        @Override
        public int getColumnCount() {
            return column;
        }

        @Override
        public int getRowCount() {
            return row;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object value = null;
            try {
                rs.absolute(rowIndex + 1);
                value = rs.getString(columnIndex + 1);// 获取表里的数据
            } catch (Exception e) {
                LOGGER.info("错误信息：" + e.getMessage());
            }
            return value;
        }
    }

    public static void main(String[] args) {
        new AllSeekers().setVisible(true);
    }
}
